<?php
/**
 *逾期案件
*/
namespace app\admin\controller;
use app\admin\controller\Admin;
use think\facade\Db;
use xhadmin\service\admin\record\CustomerService;
use xhadmin\db\Customer as CustomerDb;
use think\facade\Cache;

class Overdue extends Admin {


	/*会员管理*/
	function index(){
        if (!$this->request->isAjax()){
			return $this->display('index');
		}else{
            $limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$id_card= $this->request->param('id_card', '', 'strip_tags,trim');
			$first_name = $this->request->param('first_name', '', 'strip_tags,trim');
			$birth_year = $this->request->param('birth_year', '', 'strip_tags,trim');
			$last_name = $this->request->param('last_name', '', 'strip_tags,trim');
            $id= $this->request->param('id', '', 'strip_tags,trim');

            if(!empty($id)){
                $where['id'] = ['=',$id];
            }
            if(!empty($id_card)){
                $where['id_card'] = ['like','%'.$id_card.'%'];
            }
            if(!empty($birth_year)){
                $where['birth_day'] = ['like','%'.$birth_year.'%'];
            }
            if(!empty($first_name)){
                $where['first_name|last_name|passport_info'] = ['like','%'.$first_name.'%'];
            }
            if(!empty($last_name)){
                $where['last_name'] = ['like','%'.$last_name.'%'];
            }
            $where['status'] = ['=',5];
            $idArr = [];
            //$ids = Db::name("customer_log")->field('c_id as id')->where([['create_time','<',time()-40*3600*24],['status','=',4]])->select()->toArray();
            /*$ids = Db::name("customer")->field('id')->where([['create_time','<',time()-40],['status','=',4]])->select()->toArray();
            if(!empty($ids)) {
                $idArr = array_column($ids,'id');
                $where['id'] = ['in',$idArr];
            }*/
            //$where['update_time'] = ['<',time()-40];
            $ids = Db::name('customer_log')
                ->field('c_id')
                ->where([['create_time','<',time()-50*3600*24],['status','=',5]])
                ->group('c_id')
                ->select()->toArray();

            //$where['status'] = ['=',5];
			$limit = ($page-1) * $limit.','.$limit;
            $idArr = array_column($ids,'c_id');
            //p($idArr);exit;
			try{
                if(!empty($idArr)) {
                    if(empty($id)) {
                        $where['id'] = ['in', $idArr];
                    }
                    $res = CustomerService::pageListA(formatWhere($where),$limit,'*',$orderby = 'id desc',5);

                    foreach ($res['list'] as $key=>&$val) {
                        $val['work_year_month'] = $val['work_year'].'年'.$val['work_month'].'个月';
                        $val['name'] = $val['first_name'].' '.$val['last_name'];
                    }
                    $list = $res['list'];
                } else {
                    //exit('sas');
                    $list = [];
                }

			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}



	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','intval');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(CustomerDb::getInfo($id)));
			return $this->display('appeal/info');
		}else{
            $postField = 'id,status,id_card,idcardimg,idcardimgel,bankimg,wjbhzimg,first_name,last_name,birth_day,
			religion,addr,expiry_date,id_card_upside_no,work_year,work_month,mobile,director,is_rename,is_hz_dg,
			is_yhb_dg,is_introducer,introducer,tzsslbh';
			$data = $this->request->only(explode(',',$postField),'post');
			try {
                CustomerService::update($data);
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}


    /*改为申诉案件*/
    function appeal() {
        if ($this->request->isPost()){
            $postField = 'ids';
            $data = $this->request->only(explode(',',$postField),'post');
            try {
                $now = time();
                if(strstr($data['ids'],',')) {
                    $ids = explode(',',$data['ids']);
                    CustomerService::editWhere([['id','in',$ids]],['status'=>6]);
                    foreach ($ids as $key=>$val) {
                        $info = CustomerDb::getInfo($val);
                        $customer_log[] = [
                            'c_id' => $val,
                            'id_card' => $info['id_card'],
                            'status' => 6,
                            'create_by' => session('admin.userid'),
                            'create_time' => $now,
                        ];
                    }
                    Db::name("customer_log")->insertAll($customer_log);
                } else {
                    $id = $data['ids'];
                    $info = CustomerDb::getInfo($id);
                    $customer_log = [
                        'c_id' => $id,
                        'id_card' => $info['id_card'],
                        'status' => 6,
                        'create_by' => session('admin.userid'),
                        'create_time' => $now,
                    ];
                    Db::name("customer_log")->insert($customer_log);
                    CustomerService::update(['id'=>$id,'status'=>6]);
                }
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            return json(['status'=>'00','msg'=>'修改成功']);
        }
    }

	/*删除*/
	function delete(){
		$idx =  $this->request->post('ids', '', 'strip_tags');
		if(!$idx) $this->error('参数错误');
		try{
			$where['id'] = explode(',',$idx);
            CustomerService::delete($where);
		}catch(\Exception $e){
			$this->error($e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看数据*/
	function view(){
		$id = $this->request->get('id','','intval');
		if(!$id) $this->error('参数错误');
		try{
		    $info = checkData(CustomerDb::getInfo($id));
		    if(!empty($info['passport_info'])) {
		        $pinfo = json_decode($info['passport_info'],1);
                $pinfo = isset($pinfo['arr'])?$pinfo['arr']:[];
            } else {
                $pinfo = [];
            }
			$this->view->assign('list',$pinfo);
			return $this->display('view');
		} catch (\Exception $e){
			$this->error($e->getMessage());
		}
	}

    function getThumbPic(){
        $detail = $this->request->post('detail','','strval');
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/',$detail,$allImg);
        return json(['status'=>'00','imgurl'=>$allImg[1][0]]);
    }



}

